package com.molichuxing.services.infrastructure.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.bean.EntrepotStorageBean;
import com.molichuxing.services.infrastructure.bean.StockBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.EntrepotStorageMapper;
import com.molichuxing.services.infrastructure.dto.request.create.EntrepotStorageCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.EntrepotStorageModifyDto;
import com.molichuxing.services.infrastructure.dto.response.EntrepotStorageDto;
import com.molichuxing.services.infrastructure.dto.response.StockDto;
import com.molichuxing.services.infrastructure.service.EntrepotStorageService;
/**
 * 
 * @author liyg
 * 中转库服务实现类
 * 2019-08-20
 *
 */
@Service("entrepotStorageService")
public class EntrepotStorageServiceImpl implements EntrepotStorageService{
	
	@Autowired
	private EntrepotStorageMapper entrepotStorageMapper;

	/**
	 *获取中转库列表
	 * @param map 参数
	 *key:shortName  String 中转库简称 非必填
	 *key:name       String 负责人姓名 非必填
	 *key:regionId   Integer 城市id 非必填
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<EntrepotStorageDto> getEntrepotStorageList(Map<String, Object> map) {
		
		long total=entrepotStorageMapper.getPageCount(map);
		if(total <= 0) {
			return null;
		}
		
		Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
		Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
		List<EntrepotStorageBean> list=entrepotStorageMapper.getPage(map,pageNum,pageSize);
		List<EntrepotStorageDto> entrepotStorageDtoList = Convert.toEntrepotStorageDto(list);
		Paged<EntrepotStorageDto> paged=new Paged<>(entrepotStorageDtoList,total,pageSize);
		
		return paged;
	}

	/**
	 * 获取中转库列表
	 * 
	 * @return
	 */
	@Override
	public List<EntrepotStorageDto> getEntrepotStorageList() {
		
		List<EntrepotStorageBean> list=entrepotStorageMapper.getList();
		List<EntrepotStorageDto> entrepotStorageDtoList = Convert.toEntrepotStorageDto(list);
		
		return entrepotStorageDtoList;
	}
	
	/**
	 * 获取中转库信息
	 * @param shortName
	 * @return
	 */
	@Override
	public EntrepotStorageDto getByName(String shortName) {
		EntrepotStorageBean entrepotStorageBean =entrepotStorageMapper.getByName(shortName);
		return  Convert.toEntrepotStorageDto(entrepotStorageBean);
	}
	
	/**
	 * 获取中转库信息
	 * @param fullName
	 * @return
	 */
	@Override
	public EntrepotStorageDto getByFullName(String fullName) {
		EntrepotStorageBean entrepotStorageBean =entrepotStorageMapper.getByFullName(fullName);
		return Convert.toEntrepotStorageDto(entrepotStorageBean);
	}
	
	/**
	 * 保存中转库信息
	 * 
	 * @param createDto
	 * @return
	 */
	@Override
	public Integer create(EntrepotStorageCreateDto createDto) {
		
		EntrepotStorageBean bean=new EntrepotStorageBean();
		BeanUtils.copyProperties(createDto, bean);
		entrepotStorageMapper.create(bean);
		return  bean.getId();
	}
	
	/**
	 * 修改中转库信息
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	public Boolean modify(EntrepotStorageModifyDto modifyDto) {
		
		EntrepotStorageBean entrepotStorage = entrepotStorageMapper.getById(modifyDto.getId());
		if(null == entrepotStorage) {
			throw new BizException(101,"不存在该条信息");
		}
		
		EntrepotStorageBean bean=new EntrepotStorageBean();
		BeanUtils.copyProperties(modifyDto, bean);
		
		int num = entrepotStorageMapper.updateById(bean);
		
		return num==1 ? true : false;
	}
	
	/**
	 * 获取中转库详情
	 * 
	 * @param id 中转库id
	 * @return
	 */
	@Override
	public EntrepotStorageDto getEntrepotStorage(Integer id) {
		
		EntrepotStorageBean entrepotStorageBean = entrepotStorageMapper.getById(id);
		EntrepotStorageDto entrepotStorageDto = Convert.toEntrepotStorageDto(entrepotStorageBean);
		
		return entrepotStorageDto;
	}
	
	/**
	 * 修改中转库信息
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	public Boolean modifyFenceId(Integer fenceId, Integer id) {
		int num = entrepotStorageMapper.modifyFenceId(fenceId,id);
		return num > 0;
	}
	
	/**
	 * 获取中转库信息 模糊查询
	 * @param shortName
	 * @return
	 */
	@Override
	public List<EntrepotStorageDto> getByShortName(String shortName) {
		List<EntrepotStorageBean> list = entrepotStorageMapper.getByShortName(shortName);
		return Convert.toEntrepotStorageDto(list);
	}
	
	@Override
	public Map<Integer, EntrepotStorageDto> getEntrepotStorageList(List<Integer> ids) {

		if(null == ids || ids.isEmpty()) {
			return null;
		}
		
		List<EntrepotStorageBean> list=entrepotStorageMapper.getByIds(ids);
		List<EntrepotStorageDto> dtoList= Convert.toEntrepotStorageDto(list);
		if(null == dtoList || dtoList.isEmpty()) {
			return null;
		}
		
		int initialCapacity=dtoList.size();
		Map<Integer,EntrepotStorageDto> map=new  HashMap<>(initialCapacity);
		for (EntrepotStorageDto dto : dtoList) {
			map.put(dto.getId(), dto);
		}
		
		return map;
	}

}
